#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//#include "config.h"
//

static int sm_inner_cmd_handle_view_dp(char cmd, char dp_num); //显示大棚数据
static int sm_inner_cmd_handle_list_tel(char cmd, char param);//查询手机号
static int sm_inner_cmd_handle_resp_stat(char cmd, char param);//回复命令执行数据
static int sm_inner_cmd_handle_balance(char cmd, char param);//处理余额信息
static int sm_inner_cmd_handle_query(char cmd, char param);//发送短信查询余额
//static int sm_inner_cmd_handle_tcp_alarm(char cmd, char param);

/*内部指令*/
//
// +------+---------+--------------------------------------------------------+
// | CMD  | PARAM   |                    DESCRIPTION                         |
// +------+---------+--------------------------------------------------------+
// |  1   | 大棚号  | 查询对应大棚温湿度，包括当前值、上下限                 |
// +------+---------+--------------------------------------------------------+
// |      |   0     | 查询所有大棚的当前温湿度                               |
// +------+---------+--------------------------------------------------------+
// |  2   |   N     | 查询手机号                                             |
// +------+---------+--------------------------------------------------------+
// |  3   | 状态码  | 执行完设置命令，回发执行结果                           |
// +------+---------+--------------------------------------------------------+
// |  4   | 报警值  | 手机余额的最小值，低于最小值报警                       |
// +------+---------+--------------------------------------------------------+
// |  5   | 报警值  | 查询余额，发送查询短信                                 |
// +------+---------+--------------------------------------------------------+
// |  6   |   N     | SIM900A发生关机，重新开机、初始化                      |
// +------+---------+--------------------------------------------------------+
// |  7   |   N     | 设置系统时间，使用l_time_cclk_string                   |
// +------+---------+--------------------------------------------------------+
//内部命令处理句柄绑定
//cmd_fifo_t l_fifo_id = 0;
//
//
#define     CCLK_TIME_STRING_BUFFER_LEN 35
static char l_time_cclk_string[CCLK_TIME_STRING_BUFFER_LEN];

cmd_fifo_cmd_handle_t l_inner_cmd_handle[]=
{
    {1,sm_inner_cmd_handle_view_dp}, //查询大棚温湿度
    {2,sm_inner_cmd_handle_list_tel},//查询手机号
    {3,sm_inner_cmd_handle_resp_stat},//命令执行状态
    {4,sm_inner_cmd_handle_balance},//处理余额
    {5,sm_inner_cmd_handle_query},//发送短信查询余额
};

/*存储从短信读取的配置信息*/
static sm_set_dp_t oset_dp;

static sm_set_dp_t *l_set_dp = &oset_dp;


//存储收信人
static char l_sm_recipient[20] = {0};
/*短信缓冲区*/
static char l_sm_msg_buffer[300] = {0};
static uint8_t l_balance = 0; //话费余额


static void sm_error_handle(char type);

//==========================================
static int      sm_string_is_num(char *msg, int len);
static void     sm_string_to_upper(char *msg, int len);
static int      sm_telnum_verify(char *msg, int len);
static uint8_t  sm_character_hex2dec(char ch);
static int      sm_text_split2line_proc (char *msg, int (*callback)(char * msg, int len));

void text_conv_asc2pdu(const char *src, char *dest, int len);

int  sm_inner_cmd_push_fifo (char cmd, char param);//命令推入fifo

static int sm_msg_flush(void);
static int sm_msg_push(char *msg);

int sm_option_tel_isExist(char *num);




static int _max(int a, int b)
{
    return (a>b)?a:b;
}
static int _min(int a, int b)
{
    return (a<b)?a:b;
}


//初始化设置指针
int sm_init(option_t *option)
{
    if (option == NULL)
    {
        return -1;
    }
    l_option = option;
    //l_fifo_id;
    cmd_fifo_init (l_inner_cmd_handle, DIM(l_inner_cmd_handle));
    return 0;
}

//按索引删除手机号
int sm_option_tel_del_by_index(int total , int num, char *msg, int len)
{
    static int index = 0;
    int i;
    /*printf ("OK\n");*/
    if (num == 1)
    {
        if (sm_string_is_num (msg, len) == 0)
        {
            index = atoi (msg);
            return 0;
        }
        else
            return SM_ERROR_TEL_NO;
    }
    else if (num == 2)
    {
        if(len == 0 && index >=1 && index <= 4)
        { //执行操作
            /*memset (l_option->number[index - 1], 0, 11);*/
            for (i = index - 1; i < 3; i ++)
            {
                memcpy (l_option->telephone[i].number, 
                        l_option->telephone[i+1].number, 11);
            }
            memset (l_option->telephone[3].number, 0, 11);
            option_set (l_option);

            sm_inner_cmd_push_fifo(3, SM_OK_SETUP);
            return 0;
        }
        else
        {
            //   sm_error_handle(7);
            return SM_ERROR_SYNTAX; //语法错误
        }
    }
    else
    {
        return SM_ERROR_SYNTAX; //语法错误
    }
    //return SM_ERROR_SYNTAX; //语法错误
}


//按索引增加手机号
int sm_option_tel_add_by_index(int total , int num, char *msg, int len)
{
    static int index = 0;
    int i;
    if (num == 1) //取得索引
    {
        if (sm_string_is_num (msg, len) == 0)
        {
            index = atoi (msg); 
            return 0;
        }
        else
            return -1;
    }
    else if (num == 2) //取得号码
    {
        if(len == 11 && sm_telnum_verify(msg,len) == 0 && index >=1 && index <= 4 ) //验证数据正确性
        { 
            /*printf ("增加手机号ind %d %s\n", index, msg);*/\
                if (sm_option_tel_isExist(msg) != 0) //电话号码不存在
                {
                    for (i = 0; i < 11; i ++)
                    {
                        l_option->telephone[index - 1].number[i] = msg[i] - '0';
                    }
                    option_set (l_option);
                    sm_inner_cmd_push_fifo(3, SM_OK_SETUP); //保存成功
                    return 0;
                }
                else
                {
                    sm_inner_cmd_push_fifo(3, SM_ERROR_TEL_EXIST); //手机号码已存在
                    return 0;
                }
        }
        else
        {
            sm_inner_cmd_push_fifo(3, SM_ERROR_TEL_NO); //手机号码已存在
            return 0;
        }
    }
    return -1;
}


/*******************************************************************************
 * Function:      sm_option_tel_del()
 * Description:   删除电话号 
 * Calls:         
 * Called By:      
 * Input:         msg   --字符串
 *                len   --字符串长度
 * Return:        int
 * Others:        None
 ******************************************************************************/
int sm_option_tel_del(char *pdu_num)
{
    int i, j, k;
    char tel_num[12] = {0};
    /*printf ("[删除手机号码]");*/
    for (i = 0; i < 11; i ++)
    {
        tel_num[i] = pdu_num[4+i] - '0';
        /*printf ("%d", tel_num[i]);*/
    } 
    for (j = 0; j < 4; j ++)
    {
        if (memcmp(l_option->telephone[j].number, tel_num, 11) == 0) //比对电话号
        {
            for (k = j; k < 3; k ++)
            {
                memcpy(l_option->telephone[k].number, l_option->telephone[k+1].number, 11);
            }
            memset(l_option->telephone[3].number, 0, 11);
            j --;
        }
    }
    option_set(l_option);
    sm_inner_cmd_push_fifo(3, SM_OK_SETUP); //设置成功
    /*putchar ('\n');*/
    return 0;
}


//添加电话号
int sm_option_tel_add(char *num, int len)
{
    int i = 0;
    int j = 0;
    int ret = 0;
    if (len == 11 && sm_telnum_verify(num, len) == 0)
    { //正常处理

        if (sm_option_tel_isExist(num) != 0) //电话号码不存在
        {
            for (i = 0; i < 4; i ++)
            {
                if (l_option->telephone[i].number[0] != 1) //寻找空余位置
                {
                    for (j = 0; j < 11; j ++)
                    {
                        l_option->telephone[i].number[j] = num[j] - '0';
                    }
                    option_set (l_option);
                    sm_inner_cmd_push_fifo(3, SM_OK_SETUP); //设置成功
                    ret = 0;
                    return ret;
                }
            }
            sm_inner_cmd_push_fifo(3, SM_ERROR_TEL_OVERFLOW); //电话号已存满
            ret = 0;
        }
        else
        {
            sm_inner_cmd_push_fifo(3, SM_ERROR_TEL_EXIST); //手机号码已存在
            return 0;
        }
    }
    else
    {
        ret = SM_ERROR_TEL_NO;
    }
    return ret;
}


/*验证电话号码是否已经存在*/
/*******************************************************************************
 * Function:      sm_option_tel_isExist()
 * Description:   验证电话号码是否存在
 * Calls:          
 * Called By:      
 * Input:         num  --手机号码字符串（11个字符）
 * Return:        0    --号码已经存在
 *                -1   --号码不存在
 * Others:        None
 ******************************************************************************/
int sm_option_tel_isExist(char *num)
{
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 11; j ++)
        {
            if (l_option->telephone[i].number[j] != num[j]-'0')
            {
                break;
            }
            if (j == 10)
                return 0;
        }
    }
    return -1;
}





int sm_msg_push_int(int num)
{
    char dp_no_str[10] = {0};
    char buffer[15] = {0};
    sprintf (dp_no_str, "%d", num);
    text_conv_asc2pdu (dp_no_str, buffer, strlen (dp_no_str));
    sm_msg_push (buffer);
    return 0;
}

int sm_msg_push_float(float num)
{
    char dp_no_str[10] = {0};
    char buffer[15] = {0};
    sprintf (dp_no_str, "%.01f", num);
    text_conv_asc2pdu (dp_no_str, buffer, strlen(dp_no_str));
    sm_msg_push (buffer);
    return 0;
}


/*查看大棚数据*/
/*******************************************************************************
 * Function:      sm_option_view_dp_info()
 * Description:   通过短信查看大棚数据
 * Calls:         
 * Called By:     sm_inner_cmd_handle_view_dp()
 * Input:         dp_num    --大棚序号
 * Return:         
 * Others:        None
 ******************************************************************************/
int  sm_option_view_dp_info(int dp_num)
{

    int ret = 0;
    int i;
#if (PLATFORM == LPC1114)
    if (dp_num < 0 || dp_num > 20) //超过大棚数量范围
    {
        ret = SM_ERROR_DP_NO;    //       ,  "大棚信息输入有误\n"
    }
    else if (dp_num == 0)
    {
        /*printf("查询所有大棚温湿度信息:\n");*/
        sm_msg_push ("67E58BE262406709592768DA6E296E7F5EA64FE1606F003A000A");
        for (i = 1; i <= l_option->number; i ++)
        {
            sm_msg_push_int (i);
            /*sm_msg_push ("号大棚温度：");*/
            sm_msg_push ("53F7592768DA6E295EA6FF1A");
            sm_msg_push_float ((float)m_GreenHouse.dp[i].temperature/10);
            /*sm_msg_push ("℃");*/
            sm_msg_push ("2103");
            /*sm_msg_push ("湿度：");*/
            sm_msg_push ("6E7F5EA6FF1A");
            sm_msg_push_float ((float)m_GreenHouse.dp[i].humidity/10);
            sm_msg_push ("FF05000A");
        }
        if (i == 0)
        {
            /*printf("(无大棚信息数据)\n");*/
            sm_msg_push ("002865E0592768DA4FE1606F6570636E0029000A");
        }
        sm_msg_flush();
        ret = 0;
    }
    else
    {
        sm_msg_push_int (dp_num);
        /*sm_msg_push ("号大棚:\n实时温度：");*/
        sm_msg_push ("53F7592768DA003A000A5B9E65F66E295EA6FF1A");
        sm_msg_push_float ((float)m_GreenHouse.dp[dp_num].temperature/10);
        /*sm_msg_push ("℃（");*/
        sm_msg_push ("2103FF08");
        sm_msg_push_int (l_option->dp[dp_num].temperature_low/10);
        /*sm_msg_push ("℃-");*/
        sm_msg_push ("2103002D");
        sm_msg_push_int (l_option->dp[dp_num].temperature_high/10);
        /*sm_msg_push ("℃）。\n");*/
        sm_msg_push ("2103FF093002000A");
        /*sm_msg_push ("实时湿度：");*/
        sm_msg_push ("5B9E65F66E7F5EA6FF1A");
        sm_msg_push_float ((float)m_GreenHouse.dp[dp_num].humidity/10);
        /*sm_msg_push ("％（");*/
        sm_msg_push ("FF05FF08");
        sm_msg_push_int (l_option->dp[dp_num].humidity_low/10);
        /*sm_msg_push ("％-");*/
        sm_msg_push ("FF05002D");
        sm_msg_push_int (l_option->dp[dp_num].humidity_high/10);
        /*sm_msg_push ("％）。\n");*/
        sm_msg_push ("FF05FF093002000A");
        /*printf("实时湿度：%d％（12％-23％）。\n", 2);*/
        ret = 0;
    }
#endif
#if (PLATFORM == LINUX)
    if (dp_num < 0 || dp_num > 20) //超过大棚数量范围
    {
        ret = SM_ERROR_DP_NO;    //       ,  "大棚信息输入有误\n"
    }
    else if (dp_num == 0)
    {
        /*printf("查询所有大棚温湿度信息:\n");*/
        sm_msg_push ("67E58BE262406709592768DA6E296E7F5EA64FE1606F003A000A");
        for (i = 0; i < l_option->number; i ++)
        {
            sm_msg_push_int (i + 1);
            /*sm_msg_push ("号大棚温度：");*/
            sm_msg_push ("53F7592768DA6E295EA6FF1A");
            sm_msg_push_int (12);
            /*sm_msg_push ("℃");*/
            sm_msg_push ("2103");
            /*sm_msg_push ("湿度：");*/
            sm_msg_push ("6E7F5EA6FF1A");
            sm_msg_push_int (12);
            sm_msg_push ("FF05000A");
        }
        if (i == 0)
        {
            /*printf("(无大棚信息数据)\n");*/
            sm_msg_push ("002865E0592768DA4FE1606F6570636E0029000A");
        }
        sm_msg_flush();
        ret = 0;
    }
    else
    {
        sm_msg_push_int (dp_num);
        /*sm_msg_push ("号大棚:\n实时温度：");*/
        sm_msg_push ("53F7592768DA003A000A5B9E65F66E295EA6FF1A");
        sm_msg_push_int (dp_num);
        /*sm_msg_push ("℃（");*/
        sm_msg_push ("2103FF08");
        sm_msg_push_int (l_option->dp[dp_num].temperature_low);
        /*sm_msg_push ("℃-");*/
        sm_msg_push ("2103002D");
        sm_msg_push_int (l_option->dp[dp_num].temperature_high);
        /*sm_msg_push ("℃）。\n");*/
        sm_msg_push ("2103FF093002000A");
        /*sm_msg_push ("实时湿度：");*/
        sm_msg_push ("5B9E65F66E7F5EA6FF1A");
        sm_msg_push_int (12);
        /*sm_msg_push ("％（");*/
        sm_msg_push ("FF05FF08");
        sm_msg_push_int (l_option->dp[dp_num].humidity_low);
        /*sm_msg_push ("％-");*/
        sm_msg_push ("FF05002D");
        sm_msg_push_int (l_option->dp[dp_num].humidity_compensation);
        /*sm_msg_push ("％）。\n");*/
        sm_msg_push ("FF05FF093002000A");
        /*printf("实时湿度：%d％（12％-23％）。\n", 2);*/
        ret = 0;
    }
#endif

    return ret;
}



/*验证手机号*/
static int sm_telnum_verify(char *msg, int len)
{
    if (len != 11) //验证电话号码位数
    {
        return SM_ERROR_TEL_NO;// "电话号输入错误\n"
    }
    else
    {
        if (sm_string_is_num(msg, len) == 0 && msg[0] == '1') //电话号码正确性验证
        { //正常处理
            return 0;
        }
        else
        {
            return SM_ERROR_TEL_NO;// "电话号输入错误\n"
        }
    }
}

/*******************************************************************************
 * Function:      sm_character_hex2dec()
 * Description:   把十六进制的单个字符转换为响应的数值
 * Calls:          
 * Called By:     text_conv_pdu2asc()
 * Input:         ch  --字符 
 * Return:        字符对应的数值
 * Others:        None
 ******************************************************************************/
static uint8_t sm_character_hex2dec(char ch)
{
    if (ch >= '0' && ch <= '9')
    {
        return ch - '0';
    }
    else if (ch >= 'A' && ch <= 'F')
    {
        return ch - 'A' + 10;
    }
    else if (ch >= 'a' && ch <= 'f')
    {
        return ch - 'a' + 10;
    }
    return 0xff;
}



/*******************************************************************************
 * Function:      sm_string_is_num()
 * Description:   检查字符串是否是纯数字字符串
 * Calls:         
 * Called By:      
 * Input:         msg   --字符串
 * 
 * Return:        0   --是数字字串
 *                -1  --不是纯数字字串，包含其他字符
 * Others:        None
 ******************************************************************************/
static int sm_string_is_num(char *msg, int len)
{
    int i = 0;
    if (len == 0) return -1;
    for (i = 0 ; i < len; i ++)
    {
        if (msg[i] == '\n' || 
                msg[i] == '\r' ||
                msg[i] == ' ' ||
                (msg[i] >= '0' &&
                 msg[i] <= '9'
                )
           )
        {
            continue;
        }
        else
        {
            return -1;
        }
    }
    return 0;
}

/*******************************************************************************
 * Function:      sm_string_to_upper()
 * Description:   把字符串全变成大写
 * Calls:         
 * Called By:      
 * Input:         msg   --字符串
 *                len   --字符串长度
 * Return:        void
 * Others:        None
 ******************************************************************************/
static void sm_string_to_upper(char *msg, int len)
{
    int i;
    for (i = 0; i < len; i ++)
    {
        if (msg[i] >= 'a' && msg[i] <= 'z')
        {
            msg[i] = msg[i] - 32;
        }
    }
}



//pdu转换为相应的asc
/*******************************************************************************
 * function:      text_conv_pdu2asc()
 * description:   把pdu格式字符串转换为ascii字符串
 * calls:         sm_character_hex2dec() 
 * called by:     sm_data_parse()
 * input:         src  --pdu格式字符串
 *                dest --输出ascii字符串
 * return:        void
 * others:         
 ******************************************************************************/
void text_conv_pdu2asc(const char *src, char *dest, int len)
{
    int i;
    uint32_t t = 0;
    int po = 0;
    /*int srclen = strlen(src);*/

    for (i = 0; i < len; i += 4)
    {
        t = 0;
        t =((uint32_t)sm_character_hex2dec(src[i]) << 12) |
            ((uint32_t)sm_character_hex2dec (src[i+1]) << 8)|
            ((uint32_t)sm_character_hex2dec (src[i+2]) << 4) |
            ((uint32_t)sm_character_hex2dec (src[i+3]));
        dest[po] = t;
        po ++;
    }
    dest[po] = '\0';

    return ;
}

//asc转换为相应的pdu
/*******************************************************************************
 * function:      text_conv_asc2pdu)
 * description:   把ascii格式字符串转换为pdu字符串
 * calls:         sm_character_dec2hex() 
 * called by:     
 * input:         src  --ascii格式字符串
 *                dest --输出pdu字符串
 *                len  --ascii字符串长度
 * return:        void
 * others:         
 ******************************************************************************/
void text_conv_asc2pdu(const char *src, char *dest, int len)
{
    int i;
    char  buff[5] = {0};

    for (i = 0; i < len; i ++)
    {
        sprintf (buff, "00%02X", *(src+i));
        strcat (dest, buff);
    }
    *(dest + strlen(dest))= '\0';

    return ;
}





#if 0
#endif

#if 1
sm_error_handle_t l_sm_error_handle[]={
    {SM_ERROR_UNIT_INCONSISTENT ,  "6570636E53554F4D4E0D7EDF4E0095198BEFFF01000A"},
    {SM_ERROR_TIME_FORMAT       ,  "95F46570636E683C5F0F95198BEFFF01000A"  },
    {SM_ERROR_DP_INFO           ,  "592768DA4FE1606F8F93516567098BEF000A"},
    {SM_ERROR_DP_NO             ,  "592768DA53F78F93516567098BEFFF01000A"},
    {SM_ERROR_DATA_FORMAT       ,  "6570636E683C5F0F95198BEFFF01000A"},
    {SM_ERROR_CMD_INVALID       ,  "975E6CD563074EE4FF01000A"},
    {SM_ERROR_TEL_NO            ,  "75358BDD53F78F93516595198BEFFF01000A"},
    {SM_ERROR_SYNTAX            ,  "8BED6CD595198BEFFF01000A"},
    {SM_OK_SETUP                ,  "64CD4F5C6210529FFF01000A"},
    {SM_ERROR_TEL_OVERFLOW      ,  "75358BDD53F75DF26EE1FF01000A"},
    {SM_ERROR_TEL_EXIST         ,  "75358BDD53F778015DF25B585728FF01000A"},
};

#endif




static int sm_msg_push(char *msg)
{
    if (strlen(l_sm_msg_buffer) + strlen(msg) >= 280)
    {
        sm_msg_flush();
    }
    strcat (l_sm_msg_buffer, msg);
    return 0;
}

static int sm_msg_flush()
{
    int ret = 0;
    if (strlen (l_sm_msg_buffer) == 0)
    {
        return -1;
    }
    //printf ("收信人：%s\n", l_sm_recipient);
    //printf ("[FLUSH]\n%s", l_sm_msg_buffer);
    /*SIM900A_send_TEXT_SM (l_sm_recipient, l_sm_msg_buffer);*/
    SIM900A_send_PDU_SM (l_sm_recipient, l_sm_msg_buffer);
    memset (l_sm_msg_buffer, 0, 300);
    return ret;
}



/*获得短信缓冲区*/
char *sm_get_msg()
{
    return l_sm_msg_buffer;
}

/*获得话费余额*/
uint8_t sm_balance_get()
{
    return  l_balance;
}

/*设置话费余额*/
void sm_balance_set(uint8_t balance)
{
    l_balance = balance;
}

/*设置收信人*/
int sm_set_recipient(char *num)
{
    memset (l_sm_recipient, 0, 20);
    strncpy(l_sm_recipient, num, 20);
    if (l_sm_recipient[0] == '+')
    {
        /*strcpy (l_sm_recipient, l_sm_recipient+3);*/
        memmove(l_sm_recipient,l_sm_recipient+3, strlen (l_sm_recipient+3)+1);
    }
    /*printf ("tel:%s\n", l_sm_recipient);*/
    return 0;
}


//==========================================================================================

//命令推入fifo
/*int sm_inner_cmd_push_fifo (const sm_inner_cmd_t cmd)*/
int sm_inner_cmd_push_fifo (char cmd, char param)
{
    cmd_fifo_add(cmd, param);
    return 0;
}



static int sm_inner_cmd_handle_view_dp(char cmd, char dp_num)
{
    int ret = 0;
    ret = sm_option_view_dp_info(dp_num);
    sm_error_handle (ret);
    return ret;
}





extern int send_alarm_SM(char *PDU_msg) ;

//发送查询余额的短信
static int sm_inner_cmd_handle_query(char cmd, char param)
{
    SIM900A_query_balance ();
    return 0;
}



//处理余额信息
static int sm_inner_cmd_handle_balance(char cmd, char param)
{
    //[温湿度报警器]温馨提示    当前话费余额： 
    char *_1st = "005B6E296E7F5EA662A58B665668005D6E2999A863D0793A000A5F53524D8BDD8D394F59989DFF1A";
    //元，为保证系统正常工作，请及时补充话费。
    char *_2nd = "5143FF0C4E3A4FDD8BC17CFB7EDF6B635E385DE54F5CFF0C8BF753CA65F6886551458BDD8D393002000A";
    char balance_value_str[10] = {0};
    char buffer[15] = {0};
    char *tmp = NULL;
    sprintf (balance_value_str, "%d", param);
    text_conv_asc2pdu (balance_value_str, buffer, strlen (balance_value_str));

    tmp = (char *)calloc (1, strlen (_1st)+strlen(_2nd)+strlen(buffer)+1);

    strcat (tmp, _1st);
    strcat (tmp, buffer);
    strcat (tmp, _2nd);

    /*printf ("[话费余额]%s\n", tmp);*/
    if (SIM900A_get_monthday() >= 27) //27号以后
    {
        if (param <= 50 && param > 0) //余额
        {
            sm_balance_set(param);
            /*DBG_PRINTF("%d\n",SIM900A_gprs_send_tcp(3));*/
            if (SIM900A_gprs_send_tcp(3) == -1
                    && SIM900A_gprs_send_tcp(3) == -1
                    && SIM900A_gprs_send_tcp(3) == -1)
            {
                send_alarm_SM (tmp);
            }
        }
    }
    else //27号以前
    {
        if (param <= 10 && param > 0) //余额
        {
            sm_balance_set(param);
            DBG_PRINTF("%d\n",SIM900A_gprs_send_tcp(3));
            if (SIM900A_gprs_send_tcp(3) == -1
                    && SIM900A_gprs_send_tcp(3) == -1
                    && SIM900A_gprs_send_tcp(3) == -1)
            {
                send_alarm_SM (tmp);
            }
        }
    }
    free (tmp);
    return 0;
}



//处理执行内部命令
void sm_inner_cmd_loop()
{
    cmd_fifo_main_loop ();
    sm_msg_flush();
}



//从短信中获取"余额"字段的值
/*******************************************************************************
 * Function:      sm_query_balance ()
 * Description:   从短信中获取"余额"字段的值
 * Calls:          
 * Called By:      
 * Input:         msg  --PDU UCS2文本
 * Return:        n   --余额值
 *                -1  --错误
 * Others:        None
 ******************************************************************************/
int sm_query_balance(char *msg)
{
    char *last = NULL;
    char *find = strstr(msg, "4F59989D");//匹配'余额'
    int value = -1;

    if (find != NULL) //匹配'余额'
    {
        last = (char *)calloc(1, strlen(msg)/2+1);
        if (last == NULL) return -1;
        *(find + 24) = '\0'; //限制匹配字符串长度
        text_conv_pdu2asc(strstr(find, "003"), last, 16); //匹配数字并转换
        value = atoi(last);
        free (last);
    }
    /*printf ("ddd:%d\n", _monthday);*/
    sm_inner_cmd_push_fifo (4, value);
    return value;
}

//查询余额
int sm_query_balance_run()
{
    sm_inner_cmd_push_fifo (5, 0);
    return 0;
}


/*使用CCLK格式设置时间*/
int sm_time_set (char * time)
{
    memset (l_time_cclk_string, 0, CCLK_TIME_STRING_BUFFER_LEN);
    strcpy (l_time_cclk_string, time);
    sm_inner_cmd_push_fifo (7, 0);
    return 0;
}

